<!doctype html>



  


<html class="theme-next pisces use-motion">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>



<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />












  
  
  <link href="/vendors/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/vendors/font-awesome/css/font-awesome.min.css?v=4.4.0" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.0.1" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="Hexo, NexT" />





  <link rel="alternate" href="/atom.xml" title="liujinlongxa的技术博客" type="application/atom+xml" />




  <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=5.0.1" />






<meta name="description" content="第一章 图层树CoreAnimation简介
动画只是CoreAnimation特性的冰山一角
CoreAnimation是一个复合引擎，它的职责就是尽可能快地组合屏幕上不同的可视内容，这个内容被分解成独立的图层，存储在一个叫图层树的体系中。

图层与视图
CALayer类在概念上和UIView类似，同样是一些被层级关系树管理的矩形块，同样可以含有一些内容。他有一些方法和属性用来做动画和变换。
和">
<meta property="og:type" content="article">
<meta property="og:title" content="《ios核心动画高级技巧》阅读笔记(1)">
<meta property="og:url" content="http://yoursite.com/2015/12/27/《ios核心动画高级技巧》阅读笔记(1)/index.html">
<meta property="og:site_name" content="liujinlongxa的技术博客">
<meta property="og:description" content="第一章 图层树CoreAnimation简介
动画只是CoreAnimation特性的冰山一角
CoreAnimation是一个复合引擎，它的职责就是尽可能快地组合屏幕上不同的可视内容，这个内容被分解成独立的图层，存储在一个叫图层树的体系中。

图层与视图
CALayer类在概念上和UIView类似，同样是一些被层级关系树管理的矩形块，同样可以含有一些内容。他有一些方法和属性用来做动画和变换。
和">
<meta property="og:image" content="http://7xn88v.com1.z0.glb.clouddn.com/004ddacd5a9506600889fc2c32686262.png">
<meta property="og:image" content="http://7xn88v.com1.z0.glb.clouddn.com/01278b2b759e5b3c5fc5ae2a961a71ee.png">
<meta property="og:updated_time" content="2016-07-01T12:43:36.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="《ios核心动画高级技巧》阅读笔记(1)">
<meta name="twitter:description" content="第一章 图层树CoreAnimation简介
动画只是CoreAnimation特性的冰山一角
CoreAnimation是一个复合引擎，它的职责就是尽可能快地组合屏幕上不同的可视内容，这个内容被分解成独立的图层，存储在一个叫图层树的体系中。

图层与视图
CALayer类在概念上和UIView类似，同样是一些被层级关系树管理的矩形块，同样可以含有一些内容。他有一些方法和属性用来做动画和变换。
和">
<meta name="twitter:image" content="http://7xn88v.com1.z0.glb.clouddn.com/004ddacd5a9506600889fc2c32686262.png">



<script type="text/javascript" id="hexo.configuration">
  var NexT = window.NexT || {};
  var CONFIG = {
    scheme: 'Pisces',
    sidebar: {"position":"left","display":"post"},
    fancybox: true,
    motion: true,
    duoshuo: {
      userId: 0,
      author: '博主'
    }
  };
</script>




  <link rel="canonical" href="http://yoursite.com/2015/12/27/《ios核心动画高级技巧》阅读笔记(1)/"/>

  <title> 《ios核心动画高级技巧》阅读笔记(1) | liujinlongxa的技术博客 </title>
</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  










  
  
    
  

  <div class="container one-collumn sidebar-position-left page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-meta ">
  

  <div class="custom-logo-site-title">
    <a href="/"  class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <span class="site-title">liujinlongxa的技术博客</span>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>
  <p class="site-subtitle"></p>
</div>

<div class="site-nav-toggle">
  <button>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
  </button>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      

      
    </ul>
  

  
</nav>

 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  
  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
            
            
              
                《ios核心动画高级技巧》阅读笔记(1)
              
            
          </h1>
        

        <div class="post-meta">
          <span class="post-time">
            <span class="post-meta-item-icon">
              <i class="fa fa-calendar-o"></i>
            </span>
            <span class="post-meta-item-text">发表于</span>
            <time itemprop="dateCreated" datetime="2015-12-27T00:00:00+08:00" content="2015-12-27">
              2015-12-27
            </time>
          </span>

          
            <span class="post-category" >
              &nbsp; | &nbsp;
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
              
                <span itemprop="about" itemscope itemtype="https://schema.org/Thing">
                  <a href="/categories/读书笔记/" itemprop="url" rel="index">
                    <span itemprop="name">读书笔记</span>
                  </a>
                </span>

                
                

              
            </span>
          

          
            
              <span class="post-comments-count">
                &nbsp; | &nbsp;
                <a href="/2015/12/27/《ios核心动画高级技巧》阅读笔记(1)/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count ds-thread-count" data-thread-key="2015/12/27/《ios核心动画高级技巧》阅读笔记(1)/" itemprop="commentsCount"></span>
                </a>
              </span>
            
          

          

          
          

          
        </div>
      </header>
    


    <div class="post-body" itemprop="articleBody">

      
      

      
        <h2 id="第一章-图层树"><a href="#第一章-图层树" class="headerlink" title="第一章 图层树"></a>第一章 图层树</h2><h3 id="CoreAnimation简介"><a href="#CoreAnimation简介" class="headerlink" title="CoreAnimation简介"></a>CoreAnimation简介</h3><ul>
<li>动画只是CoreAnimation特性的冰山一角</li>
<li>CoreAnimation是一个复合引擎，它的职责就是尽可能快地组合屏幕上不同的可视内容，这个内容被分解成独立的图层，存储在一个叫图层树的体系中。</li>
</ul>
<h3 id="图层与视图"><a href="#图层与视图" class="headerlink" title="图层与视图"></a>图层与视图</h3><ul>
<li>CALayer类在概念上和UIView类似，同样是一些被层级关系树管理的矩形块，同样可以含有一些内容。他有一些方法和属性用来做动画和变换。</li>
<li>和UIView最大的不同是，CALayer不处理用户的交互。CALayer并不清楚具体的响应链，因此它并不能响应时间。</li>
<li>每个UIView都有一个CALayer属性，即所谓的bakinglayer，视图的职责就是创建并管理这个图层。视图的层级关系与其图层的层级关系是相同的。</li>
<li>真正在界面上显示和进行动画的都是CALayer，UIView只是对CALayer的一个封装而已。</li>
<li>进行视图和图层的分类，是为了职责分离，避免重复的代码。ios中视图为UIView，MacOS中视图为NSView，但是他们的图层都是CALayer。</li>
</ul>
<h3 id="图层的能力"><a href="#图层的能力" class="headerlink" title="图层的能力"></a>图层的能力</h3><ul>
<li>CALayer能做但UIView不能做的东西：<ul>
<li>阴影，圆角，带颜色的边框</li>
<li>3D变换</li>
<li>非矩形范围</li>
<li>透明遮罩</li>
<li>多级非线性动画</li>
</ul>
</li>
</ul>
<h3 id="使用图层"><a href="#使用图层" class="headerlink" title="使用图层"></a>使用图层</h3><ul>
<li>可以直接在UIView的layer属性中添加图层，如下</li>
</ul>
<figure class="highlight objectivec"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line">- (<span class="keyword">void</span>)viewDidLoad &#123;</div><div class="line">    [<span class="keyword">super</span> viewDidLoad];</div><div class="line"></div><div class="line">    <span class="built_in">CALayer</span> *blueLayer = [[<span class="built_in">CALayer</span> alloc] init];</div><div class="line">    blueLayer.frame = <span class="built_in">CGRectMake</span>(<span class="number">50</span>, <span class="number">50</span>, <span class="number">50</span>, <span class="number">50</span>);</div><div class="line">    blueLayer.backgroundColor = [<span class="built_in">UIColor</span> blueColor].CGColor;</div><div class="line">    [<span class="keyword">self</span>.layerView.layer addSublayer:blueLayer];</div><div class="line"></div><div class="line">&#125;</div></pre></td></tr></table></figure>
<ul>
<li>使用图层而不使用视图的情况<ul>
<li>开发可以在MacOS上运行的跨平台应用</li>
<li>使用多种CALayer的子类（特殊图层），并且不想创建单独的UIView去封装他们</li>
<li>做一些对性能特别挑剔的工作</li>
</ul>
</li>
</ul>
<h2 id="第二章-寄宿图"><a href="#第二章-寄宿图" class="headerlink" title="第二章 寄宿图"></a>第二章 寄宿图</h2><p>CALayer除了可以设置颜色，也可以设置一张图片，这张图片称为CALayer的寄宿图。</p>
<h3 id="contents属性"><a href="#contents属性" class="headerlink" title="contents属性"></a>contents属性</h3><ul>
<li>CALayer有一个contents的属性，它虽然是id类型，但是必须设置一个CGImage，才能显示出图片，设置为其他类型，都只会显示一个空白的区域。这种现象是有Mac OS的历史历史原因造成的。在Mac OS上，contents对CGImage和NSImage类型的值都起作用，但在ios中，不能给contents设置一个UIImage。</li>
<li>contentGravity属性：与UIView中的contentMode属性对应</li>
<li>contentScale属性：定义了寄宿图的像素尺寸和视图的比例，默认为1.0。UIView中与之对应的属性为contentScaleFactor。使用UIImage加载图片会自动根据屏幕加载2x或3x图片，但是使用CGImage则不会。这样我们就需要手动设置contentScale属性来修复这个问题。一般使用代码设置寄宿图，都会手动设置contentScale，如下：<code>blueLayer.contentsScale = [[UIScreen mainScreen] scale];</code></li>
<li>maskToBounds属性用来决定是否显示超出边界的内容</li>
</ul>
<h3 id="contentsRect属性"><a href="#contentsRect属性" class="headerlink" title="contentsRect属性"></a>contentsRect属性</h3><ul>
<li>contentRect属性允许我们在图层边框里显示寄宿图的一个子域，这要比contentGravity属性灵活许多。contentRect是一个单位坐标，长宽最大都为1。默认为{0，0，1，1}，当设置为{0，0，0.5，0.5}，则显示效果如下图：</li>
</ul>
<p><img src="http://7xn88v.com1.z0.glb.clouddn.com/004ddacd5a9506600889fc2c32686262.png" alt="contentsRect"></p>
<ul>
<li>contentRect的另外一个用途就是ImageSprites（图片拼合），把多张图片放在一张大的图片上，然后使用contentRect分别显示每一张图片，去掉不显示的部分，这就是图片拼合。在游戏编程中非常常见。加载一张大图要比加载多张小图效率要高。</li>
</ul>
<h3 id="contentsCenter属性"><a href="#contentsCenter属性" class="headerlink" title="contentsCenter属性"></a>contentsCenter属性</h3><ul>
<li>contentsCenter属性表示图层相对于寄宿图的位置与大小，可以用来定义图片的拉伸。</li>
<li>如下图，虚线框为图层的区域，灰色为内容的区域。</li>
</ul>
<p><img src="http://7xn88v.com1.z0.glb.clouddn.com/01278b2b759e5b3c5fc5ae2a961a71ee.png" alt="contentCenter"></p>
<ul>
<li>该属性相当于UIImage的<code>resizableImageWithCapInsets:</code>方法</li>
</ul>
<h3 id="customDrawing"><a href="#customDrawing" class="headerlink" title="customDrawing"></a>customDrawing</h3><ul>
<li>除了直接给contents属性设置CGImage，也可以通过CoreGraphics直接绘制寄宿图。使用UIVIew 的drawRect:方法来自定义绘制</li>
<li>除了重写drawRect:方法，也可以通过设置CALayer的代理，使用代理方法<code>(void)displayLayer:</code>来实现重绘，但是强烈建议使用前一种方法。</li>
</ul>

      
    </div>

    <div>
      
        
      
    </div>

    <div>
      
        

      
    </div>

    <footer class="post-footer">
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2015/12/20/RAC学习笔记/" rel="next" title="RAC学习笔记">
                <i class="fa fa-chevron-left"></i> RAC学习笔记
              </a>
            
          </div>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2015/12/27/《ios核心动画高级技巧》阅读笔记(2)/" rel="prev" title="《ios核心动画高级技巧》阅读笔记(2)">
                《ios核心动画高级技巧》阅读笔记(2) <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          
  <div class="comments" id="comments">
    
      <div class="ds-thread" data-thread-key="2015/12/27/《ios核心动画高级技巧》阅读笔记(1)/"
           data-title="《ios核心动画高级技巧》阅读笔记(1)" data-url="http://yoursite.com/2015/12/27/《ios核心动画高级技巧》阅读笔记(1)/">
      </div>
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel ">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="/images/smallhead.png"
               alt="liujinlongxa" />
          <p class="site-author-name" itemprop="name">liujinlongxa</p>
          <p class="site-description motion-element" itemprop="description">生命永不息，Coding永不止</p>
        </div>
        <nav class="site-state motion-element">
          <div class="site-state-item site-state-posts">
            <a href="/archives">
              <span class="site-state-item-count">31</span>
              <span class="site-state-item-name">日志</span>
            </a>
          </div>

          
            <div class="site-state-item site-state-categories">
              <a href="/categories">
                <span class="site-state-item-count">3</span>
                <span class="site-state-item-name">分类</span>
              </a>
            </div>
          

          
            <div class="site-state-item site-state-tags">
              <a href="/tags">
                <span class="site-state-item-count">3</span>
                <span class="site-state-item-name">标签</span>
              </a>
            </div>
          

        </nav>

        
          <div class="feed-link motion-element">
            <a href="/atom.xml" rel="alternate">
              <i class="fa fa-rss"></i>
              RSS
            </a>
          </div>
        

        <div class="links-of-author motion-element">
          
            
              <span class="links-of-author-item">
                <a href="https://github.com/liujinlongxa" target="_blank" title="GitHub">
                  
                    <i class="fa fa-fw fa-github"></i>
                  
                  GitHub
                </a>
              </span>
            
              <span class="links-of-author-item">
                <a href="https://twitter.com/JaylonLiu" target="_blank" title="Twitter">
                  
                    <i class="fa fa-fw fa-twitter"></i>
                  
                  Twitter
                </a>
              </span>
            
              <span class="links-of-author-item">
                <a href="http://weibo.com/u/1523828165/home" target="_blank" title="微博">
                  
                    <i class="fa fa-fw fa-globe"></i>
                  
                  微博
                </a>
              </span>
            
              <span class="links-of-author-item">
                <a href="https://www.douban.com/people/2470188/" target="_blank" title="豆瓣">
                  
                    <i class="fa fa-fw fa-globe"></i>
                  
                  豆瓣
                </a>
              </span>
            
          
        </div>

        
        

        
        

      </section>

      
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">
            
              
            
            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#第一章-图层树"><span class="nav-number">1.</span> <span class="nav-text">第一章 图层树</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#CoreAnimation简介"><span class="nav-number">1.1.</span> <span class="nav-text">CoreAnimation简介</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#图层与视图"><span class="nav-number">1.2.</span> <span class="nav-text">图层与视图</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#图层的能力"><span class="nav-number">1.3.</span> <span class="nav-text">图层的能力</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#使用图层"><span class="nav-number">1.4.</span> <span class="nav-text">使用图层</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#第二章-寄宿图"><span class="nav-number">2.</span> <span class="nav-text">第二章 寄宿图</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#contents属性"><span class="nav-number">2.1.</span> <span class="nav-text">contents属性</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#contentsRect属性"><span class="nav-number">2.2.</span> <span class="nav-text">contentsRect属性</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#contentsCenter属性"><span class="nav-number">2.3.</span> <span class="nav-text">contentsCenter属性</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#customDrawing"><span class="nav-number">2.4.</span> <span class="nav-text">customDrawing</span></a></li></ol></li></ol></div>
            
          </div>
        </section>
      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy; 
  <span itemprop="copyrightYear">2016</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">liujinlongxa</span>
</div>

<div class="powered-by">
  由 <a class="theme-link" href="http://hexo.io">Hexo</a> 强力驱动
</div>

<div class="theme-info">
  主题 -
  <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next">
    NexT.Pisces
  </a>
</div>

        

        
      </div>
    </footer>

    <div class="back-to-top">
      <i class="fa fa-arrow-up"></i>
    </div>
  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  



  
  <script type="text/javascript" src="/vendors/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/vendors/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/vendors/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/vendors/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/vendors/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/vendors/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.0.1"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.0.1"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.0.1"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.0.1"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.0.1"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.0.1"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.0.1"></script>



  

  
    
  

  <script type="text/javascript">
    var duoshuoQuery = {short_name:"liujinlongxa"};
    (function() {
      var ds = document.createElement('script');
      ds.type = 'text/javascript';ds.async = true;
      ds.id = 'duoshuo-script';
      ds.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') + '//static.duoshuo.com/embed.js';
      ds.charset = 'UTF-8';
      (document.getElementsByTagName('head')[0]
      || document.getElementsByTagName('body')[0]).appendChild(ds);
    })();
  </script>

  
    
  






  
  
  

  

  

</body>
</html>
